/*
 * Created on 2005/05/16
 * Author aki@www.xucker.jpn.org
 * License Apache2.0 or Common Public License
 */
package jp.sourceforge.akjrcp.ui.draw2d;

import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FlowLayout;
import org.eclipse.draw2d.Graphics;

import java.util.Iterator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 
 *
 */
public class ChainedFlowFigure extends DoubleScalableFigure{
public static Log log = LogFactory.getLog(ChainedFlowFigure.class);    
private ChainedFigure lastFigure;
private FlowLayout flowLayout;

public ChainedFlowFigure(){
    super(256,256);//TODO change it.
    flowLayout=new FlowLayout(true);
    flowLayout.setMajorSpacing(0);
    flowLayout.setMinorSpacing(0);
   // flowLayout.setMajorAlignment(0);
    //flowLayout.setMinorAlignment(0);
    //flowLayout.set
    setLayoutManager(flowLayout);
    
}

public void addChainedFigure(ChainedFigure figure){
    figure.setFigure(lastFigure);
    lastFigure=figure;
    this.add(figure);
    resize();
}

public void resize(){
    //TODO vertical mode.
    int maxheight=0;
    int width=0;
    Iterator list=getChildren().iterator();
    while(list.hasNext()){
        Figure f=(Figure)list.next();
        width+=f.getSize().width;
        maxheight=Math.max(maxheight,f.getSize().height);
    }
    setSize(width,maxheight);
    //revalidate();
}

public void setScale(double wzoom,double vzoom){
    this.hzoom=vzoom;
    this.wzoom=wzoom;
    this.getChildren();
    synchronized(this){
    DoubleScalableFigure[] figures=(DoubleScalableFigure[]) getChildren().toArray(new DoubleScalableFigure[getChildren().size()]);
    for (int i = 0; i < figures.length; i++) {
        figures[i].setScale(wzoom,vzoom);
    }
    this.getLayoutManager().layout(this);
    this.revalidate();//resize
    }
    
    }

public void paintFigure(Graphics g) {
    /*
    g.setBackgroundColor(ColorConstants.darkGray);
    g.fillRectangle(getClientArea());
    if(lastFigure!=null){
        log.infolast:"+lastFigure.getClientArea());
    }
    log.infomin:"+flowLayout.getMinorSpacing()+","+flowLayout.getMajorSpacing());
    */
}

}
